#include <QMetaObject>
#include <QMetaProperty>
#include "oxford-word-usage-example.h"
#include "json-serializator.h"
#include "json-deserializator.h"
#include "serializator.h"
#include "deserialization-error.h"

using namespace Oxford;

bool WordUsageExample::isValid() const
{
    return true;
}

ObjectString WordUsageExample::toString() const
{
    Core::JSONSerializator serializator;

    serializator.beginObject();

    serializator.writeProperty("wordUsage", _wordUsage);
    serializator.writeProperty("sentence", _sentence);
    serializator.writeProperty("clarification", _clarification);

    serializator.endObject();

    return serializator.result();
}

WordUsageExample WordUsageExample::fromString(QString data)
{
    Core::JSONDeserializator deserializator(data);

    WordUsageExample result;
    const QMetaObject* meta = result.metaObject();
    try
    {
        deserializator.beginObject();

        while (!deserializator.atEnd())
        {
            QString propertyName = deserializator.readPropertyName();
            int propertyId = meta->indexOfProperty(propertyName.toStdString().c_str());

            if (propertyId < 0)
                continue;

            Core::IDeserializator::PropertyType propertyType = deserializator.readPropertyType();

            switch (propertyType)
            {
            case Core::IDeserializator::Variant:
                meta->property(propertyId).write(&result, deserializator.readPropertyValue());
                break;

            case Core::IDeserializator::Object:
                break;

            case Core::IDeserializator::List:
                break;
            }
        }

        deserializator.endObject();
        return result;
    }
    catch (Core::EDeserializationError)
    {

    }
    catch(...)
    {

    }

    return WordUsageExample();
}
